package com.zhang.common.exception.handler;

import com.zhang.common.exception.myexception.CaptchaException;
import com.zhang.common.exception.myexception.ProductUpException;
import com.zhang.common.exception.myexception.StateException;
import com.zhang.common.exception.myexception.WareLockInsufficientException;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
@ResponseBody
public class myHandler {
    @ExceptionHandler(value = CaptchaException.class)
    public R captchaException(CaptchaException e) {
        log.error("验证码错误", e);
        return RUtils.err(403, "验证码错误");
    }

    @ExceptionHandler(value = StateException.class)
    public R stateException(StateException e) {
        log.error("系统状态异常", e);
        return RUtils.err(403, e.getMessage());
    }
    @ExceptionHandler(value = WareLockInsufficientException.class)
    public R WareLockInsufficientException(WareLockInsufficientException e) {
        log.error("库存不足,抛出异常", e);
        return RUtils.err(403, "库存不足!");
    }
    @ExceptionHandler(value = ProductUpException.class)
    public R ProductUpException(ProductUpException e) {
        log.error("商品上架失败", e);
        return RUtils.err(403, "商品上架失败!");
    }
}
